package com.base.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;



public class ViewPagerAdapter extends PagerAdapter {
    private ArrayList<View> mViewList = null;
    private ArrayList<String> mTitleList;

    public ViewPagerAdapter(ArrayList<View> viewList) {
        mViewList = viewList;
    }

    public ViewPagerAdapter(ArrayList<View> viewList, ArrayList<String> titleList) {
        mViewList = viewList;
        mTitleList = titleList;
    }

    public ViewPagerAdapter(View... views) {
        mViewList = new ArrayList<>();
        for (View view : views) {
            mViewList.add(view);
        }
    }

    public void setTitles(String... titles) {
        mTitleList = new ArrayList<>();
        for (String title : titles) {
            mTitleList.add(title);
        }
    }

    @Override
    public int getCount() {
        return mViewList == null ? 0 : mViewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return (mTitleList == null || position >= mTitleList.size()) ? null : mTitleList.get(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = mViewList.get(position);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
